This invention relates to a method and apparatus for shading and texturing three dimensional computer generated graphic images and in particular to methods and apparatus which generate so called xe2x80x9cvolumetricxe2x80x9d effects through the use of depth based blending. For example a cloud, where the degree to which the cloud or any object contained partially or fully within is visible, is dependent on how much of the cloud a ray cast from the eye must pass through before either exiting the cloud or xe2x80x9cimpactingxe2x80x9d another object.
Software blending of volumes can be derived from the distance between the volume""s front vertices and its back faces, or an object that lies between them. This is achieved by casting a ray through each front facing vertex, and determining the distance it travels before impacting either a back face of the volume or an object contained within. This distance can then be used as an alpha value for the vertex which the ray was cast through. These per vertex blend factors can then be interpolated across the surfaces of the volume. However, as the blend factors are calculated per vertex any irregularities that occur in between them will not be taken into account. Thus, to accurately render a scene, the volume objects must contain a large number of vertices, or, in the extreme case, a vertex per screen pixel that is covered by the volume. This large number of vertices coupled with the fact that a ray must be cast through and tested against a large number of other object faces make this technique extremely inefficient.
Most 3D graphic systems operate by using the well known technique of Z or depth buffering. Objects to be rendered into a scene have depth values associated with them which can be used to derive a depth value for each pixel intersected by the object. These are compared with the current depth values in the depth buffer for each pixel. If an object is found to be in front of the current stored depth a new value can be overwritten into the depth buffer.
Many 3D graphics systems also use the well known technique of stencil buffering. This technique utilises a second parallel buffer that will typically contain between one and eight bits per pixel. When an object is to be written into a scene, in addition to the depth buffer comparison summarised above, a stencil buffer comparison with the current stencil buffer value is made for each pixel and a new stencil buffer value may be written. This comparison is used to select one of a set of stencil operations which are performed on the stencil buffer. These are designated as follows:
SOP1=stencil test fails,
SOP2=stencil test passes and depth test fails,
SOP3=stencil and depth tests both pass.
Thus, if the stencil test passes and the depth test fails, operation SOP2 will be assigned to the stencil buffers. Examples of the operations which can be assigned to SOP1, SOP2 and SOP3 are as follows:
Keep=keep Stencil value,
zero=replace stencil value with zero,
Replace=replace stencil value with reference value,
INCRSAT=increment stencil with clamping at a maximum value,
DECRSAT=decrement stencil with clamping at a minimum value,
Invert=invert stencil value,
INCR=increment stencil value,
DECR=decrement stencil value.
Thus, each object in the supplied object list will have a particular set of functions assigned to SOP1, SOP2 and SOP3. The object stencil value and the value within the stencil buffer are then combined using the selected operation in dependence on the stencil and depth tests. The result is then written back to the stencil buffer. The depth is subsequently only updated if both the stencil and depth buffer test succeed.
A preferred embodiment of the present invention provides a method and apparatus, which are able to implement volumetric effects, such as forming clouds, efficiently. To do this it provides a set of depth buffer operations which allow depth values to be manipulated arithmetically. These operations allow a depth or blending value to be formed that can be representative of the distance between the front and back of the volume. After derivation, these values can be passed to a texture blending unit in which they can be used to blend other components such as iterated colours, textures, or any other source applicable to texture blending. The result from the texture blending unit can then be alpha blended with the current contents of the frame buffer.